// ========================================================
// Des：
// Autor：Mr.H 
// CreateTime：2024/04/06 22:01:50
// 版 本：v 1.0
// ========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletObj : MonoBehaviour
{
    public float moveSpd = 50;
    public TankBaseObj fatherObj;//子弹拥有者
    public GameObject effectObj;

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * moveSpd * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        //子弹打墙壁  不同阵营坦克时爆炸
        if (other.gameObject.CompareTag("Cube") ||
        other.gameObject.CompareTag("Player") && fatherObj.CompareTag("Monster") ||
        other.gameObject.CompareTag("Monster") && fatherObj.CompareTag("Player"))
        {
            //判断是否受伤
            //得到碰撞到的对象身上 是否有坦克相关脚本 我们用里氏替换原则
            //通过父类去获取
            TankBaseObj obj = other.GetComponent<TankBaseObj>();
            //判断 对象上 是否挂载了 坦克相关脚本 
            if (obj != null)
                obj.Wound(fatherObj);


            //子弹销毁时 创建爆炸特效
            if (effectObj != null)
            {
                GameObject eff = Instantiate(effectObj, transform.position, transform.rotation);
                //改变音量
                AudioSource audioSource = eff.GetComponent<AudioSource>();
                //设置大小
                audioSource.volume = GameDataMgr.Instance.musicData.soundValue;
                audioSource.mute = !GameDataMgr.Instance.musicData.isOpenSound;
                //Destroy(eff);
            }
            Destroy(gameObject);
        }
    }
    //设置拥有者
    public void SetFather(TankBaseObj obj)
    {
        fatherObj = obj;
    }
}
